<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-row :gutter="48">
              <a-col :md="12" :sm="24" style="padding-left: 10px;padding-right: 0px">
                <a-form-item label="类型">
                  <a-select v-model="appTypeId" placeholder="请选择" @select="typeClick">
                    <a-select-option :value="item.value" v-for="(item,index) in optionType" :key="index" >{{ item.label }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24" style="padding-left: 10px;padding-right: 0px">
                <a-form-item label="状态">
                  <a-select v-model="status" placeholder="请选择" @select="statusClick">
                    <a-select-option :value="item.value" v-for="(item,index) in optionStatus" :key="index" >{{ item.label }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-range-picker
              :ranges="{' 今天': [moment(), moment()], '一月内': [moment(), moment().endOf('month')] }"
              @change="onChange"
            />
          </a-col>
          <a-col :md="8" :sm="24">
            <a-input-search placeholder="请输入类型关键字" @search="onSearch" >
              <a-button type="primary" slot="enterButton" icon="search">搜索</a-button>
            </a-input-search>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :pagination="pagination"
    >
      <span slot="packageName" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="name" slot-scope="text">
        <ellipsis :length="15" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="developerName" slot-scope="text">
        <ellipsis :length="15" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="appTypeName" slot-scope="text">
        <ellipsis :length="15" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="status" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
      <span slot="version" slot-scope="text">
        <ellipsis :length="15" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="downloadUrl" slot-scope="text">
        <template>
          <img style="width:25px;height:25px;cursor: pointer;" src="../../assets/icons/d.png" alt="" @click="downClick(text)">
        </template>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleLook(record)">查看</a>
          <a-divider type="vertical" />
          <a @click="handlePass(record)">通过</a>
          <a-divider type="vertical" />
          <a @click="handleSub(record)">拒绝</a>
        </template>
      </span>
    </s-table>
    <look-model ref="modal"></look-model>
    <pass-model ref="modalPass" @ok="handleOk"></pass-model>
    <not-model ref="modalNot" @ok="handleOk"></not-model>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { auditList } from '@/api/audit'
import LookModel from './modules/LookModel'
import PassModel from './modules/PassModel'
import NotModel from './modules/NotModel'
import utils from '@/utils/common'
import { mapActions } from 'vuex'
import moment from 'moment'
var timeStamp = Number(new Date())

const statusMap = {
  0: {
    status: 'processing',
    text: '审核中'
  },
  1: {
    status: 'success',
    text: '审核成功'
  },
  2: {
    status: 'warning',
    text: '审核失败'
  },
  3: {
    status: 'success',
    text: '已上架'
  },
  4: {
    status: 'default',
    text: '已下架'
  }
}

export default {
  name: 'ManageList',
  components: {
    STable,
    Ellipsis,
    LookModel,
    PassModel,
    NotModel
  },
  data () {
    return {
      status: '',
      optionStatus: [
        { value: '', label: '全部' },
        { value: '0', label: '审核中' },
        { value: '1', label: '审核成功' },
        { value: '2', label: '审核失败' },
        { value: '3', label: '已上架' },
        { value: '4', label: '已下架' }
      ],
      appTypeId: '1',
      optionType: [
        { value: '1', label: '软件' },
        { value: '2', label: '游戏' }
      ],
      // 查询参数
      queryParam: {},
      keyword: '',
      startTime: '',
      endTime: '',
      pagination: {
        current: 1,
        pageSize: 20
      },
      // 表头
      columns: [
        {
          title: 'APK包名',
          dataIndex: 'packageName',
          scopedSlots: { customRender: 'packageName' }
        },
        {
          title: '应用名称',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '开发商',
          dataIndex: 'developerName',
          scopedSlots: { customRender: 'developerName' }
        },
        {
          title: '类型',
          dataIndex: 'appTypeName',
          scopedSlots: { customRender: 'appTypeName' }
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '版本号',
          dataIndex: 'version',
          scopedSlots: { customRender: 'version' }
        },
        {
          title: '下载',
          dataIndex: 'downloadUrl',
          scopedSlots: { customRender: 'downloadUrl' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: pagination => {
        return auditList(Object.assign(pagination, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created () {
    this.queryParam = {
      token: utils.AuthToken(timeStamp, this.$store.getters.userKey),
      timestamp: timeStamp,
      adminId: this.$store.getters.adminId,
      keyword: this.keyword,
      appTypeId: this.appTypeId,
      status: this.status,
      startTime: this.startTime,
      endTime: this.endTime
    }
  },
  methods: {
    ...mapActions(['AuditPass', 'GetInto']),
    moment,
    statusClick (val) { // 状态
      this.queryParam.status = val
      this.$refs.table.refresh()
    },
    typeClick (val) { // 类型
      this.queryParam.appTypeId = val
      this.$refs.table.refresh()
    },
    onSearch (val) { // 搜索
      this.queryParam.keyword = val
      this.$refs.table.refresh()
    },
    onChange (dates, dateStrings) { // 日期
      this.queryParam.startTime = dateStrings[0]
      this.queryParam.endTime = dateStrings[1]
      this.$refs.table.refresh()
    },
    downClick (downloadUrl) { // 下载
      if (downloadUrl) {
        window.location.href = downloadUrl
      }
    },
    handleLook (record) { // 查看
      this.GetInto({ id: record.id })
        .then((res) => {
          if (res.status === 200 || res.status === '200') {
            this.$refs.modal.look(res.data)
          } else {
            this.requestFailed(res)
          }
        }).catch(err => this.requestFailed(err))
    },
    handlePass (record) { // 通过
      if (record.status === 1) {
        this.$notification['warning']({
          message: '提示',
          description: '应用已审核通过',
          duration: 4
        })
      } else if (record.status === 3) {
        this.$notification['warning']({
          message: '提示',
          description: '应用已上架',
          duration: 4
        })
      } else if (record.status === 4) {
        this.$notification['warning']({
          message: '提示',
          description: '应用已下架',
          duration: 4
        })
      } else {
        this.$refs.modalPass.pass(record)
      }
    },
    handleSub (record) { // 拒绝
      if (record.status === 3) {
        this.$notification['warning']({
          message: '提示',
          description: '应用已上架',
          duration: 4
        })
      } else if (record.status === 2) {
        this.$notification['warning']({
          message: '提示',
          description: '应用已审核失败',
          duration: 4
        })
      } else if (record.status === 4) {
        this.$notification['warning']({
          message: '提示',
          description: '应用已下架',
          duration: 4
        })
      } else {
        this.$refs.modalNot.not(record)
      }
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
      this.registerBtn = false
    }
  }
}
</script>
